/*
 * Copyright (C) 2015 soud
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <getopt.h>
#include <sys/stat.h>

#include "erupt.h"
#include "parser.h"

static char *read_file(const char *path);
static int get_options(int argc, char *argv[]);

static const char *USAGE =
"Usage: erupt [options] file\n"
"       -o, --output\n"
"               set output name (default: main)\n"
"       -v, --version\n"
"               show version\n"
"       -V, --verbose\n"
"               verbose mode\n"
"       -t, --tokens\n"
"               show generated token stream\n"
"       -n, --nodes\n"
"               show nodes of the generated AST\n"
"       -h, --help\n"
"               show this\n";

int main(int argc, char *argv[])
{
        if (!(optind < argc))
                erupt_fatal_error("an input file is required.\n");

        set_output_name(argv[optind]);

        if (get_options(argc, argv) != ERUPT_OK)
                return ERUPT_ERROR;

        TARGET_FILE = argv[optind];

        char *input_file = read_file(argv[optind]);
        lexer_t *lexer = lex(argv[optind], input_file);

        if (SHOW_TOKENS)
                dump_tokens(lexer->ts);

        free(input_file);

        ast_node_list_t *ast = parse_token_stream(lexer->ts);

        if (SHOW_NODES)
                dump_node_list(ast);

        if (PARSE_FAILED) {
                destroy_lexer(lexer);
                destroy_ast(ast);

                erupt_fatal_error("error(s) occured, stopping compilation.\n");
        }

        destroy_lexer(lexer);
        destroy_ast(ast);

        return ERUPT_OK;
}

static char *read_file(const char *path)
{
        struct stat s;
        FILE *handler = fopen(path, "r");
        char *buffer;
        size_t read;

        stat(path, &s);

        if (!handler)
                erupt_fatal_error("couldn't read file '%s'\n", path);

        if (S_ISDIR(s.st_mode))
                erupt_fatal_error("'%s' is a directory\n", path);

        buffer = smalloc(MAX_FILE_SIZE);
        read   = fread(buffer, sizeof(char), MAX_FILE_SIZE, handler);

        buffer[read] = '\0';

        fclose(handler);

        return buffer;
}

static int get_options(int argc, char *argv[])
{
        int choice;

        while (1) {
                struct option long_options[] = {
                        {"output"  , required_argument , NULL , 'o'},
                        {"version" , no_argument       , NULL , 'v'},
                        {"verbose" , no_argument       , NULL , 'V'},
                        {"tokens"  , no_argument       , NULL , 't'},
                        {"nodes"   , no_argument       , NULL , 'n'},
                        {"help"    , no_argument       , NULL , 'h'},
                        {0         , 0                 , 0    ,  0 }
                };

                int option_index = 0;

                choice = getopt_long(argc, argv, "o:vVtnh", long_options, &option_index);

                if (choice == -1)
                        break;

                switch(choice) {
                        case 'v':
                                fprintf(stderr, "Erupt %s\n", ERUPT_VERSION);
                                return ERUPT_ERROR;
                        case 'o':
                                OUTPUT_NAME = optarg;
                                break;

                        case 'h':
                                fputs(USAGE, stderr);
                                return ERUPT_ERROR;

                        case 'V':
                                VERBOSE = true;
                                break;

                        case 't':
                                SHOW_TOKENS = true;
                                break;

                        case 'n':
                                SHOW_NODES = true;
                                break;

                        default:
                                fputs(USAGE, stderr);
                                return ERUPT_ERROR;
                }
        }

        return ERUPT_OK;
}
